<template>
  <el-card class="order-container">
    <template #header>
      <div class="header">
        <el-input style="width: 200px; margin-right: 10px" placeholder="请输入订单号" v-model="state.orderNo"
          @change="handleOption" clearable />
        <el-select @change="handleOption" v-model="state.orderStatus" style="width: 200px; margin-right: 10px">
          <el-option v-for="item in state.options" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
        <!-- <el-button type="primary" size="small" icon="el-icon-edit">修改订单</el-button> -->
        <el-button type="primary" :icon="HomeFilled" @click="handleConfig()">配货完成</el-button>
        <el-button type="primary" :icon="HomeFilled" @click="handleSend()">出库</el-button>
        <el-button type="danger" :icon="Delete" @click="handleClose()">关闭订单</el-button>
      </div>
    </template>
    <el-table :load="state.loading" :data="state.tableData.slice((currentPage - 1) * pageSize, currentPage * pageSize)"
      tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column prop="orderNo" label="订单号">
      </el-table-column>
      <el-table-column prop="totalPrice" label="订单总价">
      </el-table-column>
      <el-table-column prop="orderStatus" label="订单状态">
        <template #default="scope">
          <span>{{ $filters.orderMap(scope.row.orderStatus) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="payType" label="支付方式">
        <template #default='scope'>
          <span>支付宝支付</span>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间">
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-popconfirm v-if="scope.row.orderStatus == 2" title="确定配货完成吗？" @confirm="handleConfig(scope.row.orderId)"
            confirm-button-text="确定" cancel-button-text="取消">
            <template #reference>
              <a style="cursor: pointer; margin-right: 10px">配货完成</a>
            </template>
          </el-popconfirm>
          <el-popconfirm v-if="scope.row.orderStatus == 3" title="确定出库吗？" @confirm="handleSend(scope.row.orderId)"
            confirm-button-text="确定" cancel-button-text="取消">
            <template #reference>
              <a style="cursor: pointer; margin-right: 10px">出库</a>
            </template>
          </el-popconfirm>
          <el-popconfirm v-if="!(scope.row.orderStatus == 5 || scope.row.orderStatus < 0)" title="确定关闭订单吗？"
            @confirm="handleClose(scope.row.orderId)" confirm-button-text="确定" cancel-button-text="取消">
            <template #reference>
              <a style="cursor: pointer; margin-right: 10px">关闭订单</a>
            </template>
          </el-popconfirm>
          <router-link :to="{ path: '/admin/order_detail', query: { id: scope.row.orderId } }">订单详情</router-link>
        </template>
      </el-table-column>
    </el-table>
    <!--总数超过一页，再展示分页器-->
    <div class="pages">
      <div class="page_wrap">
        <span class="page_span1">
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
            :page-sizes="[10, 20]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
            :total="state.tableData.length">
          </el-pagination>
        </span>
      </div>
    </div>
  </el-card>
</template>

<script setup>
import { onMounted, reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { HomeFilled, Delete } from '@element-plus/icons-vue'
import axios from 'axios'
const currentPage = ref(1)
const total = ref(0)
const pageSize = ref(10)

const handleSizeChange = (val) => {
  console.log(`每页 ${val} 条`);
  currentPage.value = 1;
  pageSize.value = val;
}

const handleCurrentChange = (val) => {
  console.log(`当前页: ${val}`);
  currentPage.value = val;
}
const state = reactive({
  loading: false,
  tableData: [], // 数据列表
  multipleSelection: [], // 选中项
  total: 0, // 总条数
  currentPage: 1, // 当前页
  pageSize: 10, // 分页大小
  orderNo: '', // 订单号
  orderStatus: '', // 订单状态
  // 订单状态筛选项默认值
  options: [{
    value: '',
    label: '全部'
  }, {
    value: 1,
    label: '待支付'
  }, {
    value: 2,
    label: '已支付'
  }, {
    value: 3,
    label: '配货完成'
  }, {
    value: 4,
    label: '出库成功'
  }, {
    value: 5,
    label: '交易成功'
  }, {
    value: -1,
    label: '手动关闭'
  }]
})
// 初始化获取订单列表
onMounted(() => {
  getOrderList()
})
// 获取列表方法
const getOrderList = () => {
  state.loading = true
  axios.post('http://localhost:8080/order/selectAll', {
    "orderNo": state.orderNo,
    "orderStatus": state.orderStatus
  }).then(res => {
    state.tableData = res.data
  })
}
// 触发过滤项方法
const handleOption = () => {
  state.currentPage = 1
  getOrderList()
}
// checkbox 选择项
const handleSelectionChange = (val) => {
  state.multipleSelection = val
}
// 配货方法
const handleConfig = (id) => {
  let params
  // 当个配置
  if (id) {
    params = [id]
  } else {
    if (!state.multipleSelection.length) {
      console.log('state.multipleSelection', state.multipleSelection.length)
      ElMessage.error('请选择项')
      return
    }
    // 多选配置
    params = state.multipleSelection.map(i => i.orderId)

  }
  params.forEach(item => {

    axios.post('http://localhost:8080/order/checkDone/' + item).then((res) => {
      if (res.data) {
        ElMessage.success('配货成功')
        getOrderList()
      } else {
        ElMessage.error('订单的状态不是支付成功无法执行配货操作')
      }

    })
  })
}
// 出库方法
const handleSend = (id) => {
  let params
  if (id) {
    params = [id]
  } else {
    if (!state.multipleSelection.length) {
      ElMessage.error('请选择项')
      return
    }
    params = state.multipleSelection.map(i => i.orderId)

  }
  params.forEach(item => {
    axios.post('http://localhost:8080/order/checkOut/' + item).then((res) => {
      if (res.data) {
        ElMessage.success('出库成功')
        getOrderList()
      } else {
        ElMessage.error('订单的状态不是支付成功或配货完成无法执行出库操作')
      }
    })
  })
}
// 关闭订单方法
const handleClose = (id) => {
  let params
  if (id) {
    params = [id]
  } else {
    if (!state.multipleSelection.length) {
      ElMessage.error('请选择项')
      return
    }
    params = state.multipleSelection.map(i => i.orderId)
  }
  params.forEach(item => {
    axios.post('http://localhost:8080/order/close/' + item).then(() => {
      ElMessage.success('关闭成功')
      getOrderList()
    })
  })

}
</script>